<!DOCTYPE html>
<html>
<head hexo-theme='https://github.com/volantis-x/hexo-theme-volantis/tree/4.0.0-rc.4'>
  <meta charset="utf-8">
  <!-- SEO相关 -->
  
    
  
  <!-- 渲染优化 -->
  <meta name="renderer" content="webkit">
  <meta name="force-rendering" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="HandheldFriendly" content="True" >
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta http-equiv='x-dns-prefetch-control' content='on' />

  <!-- 页面元数据 -->
  
  <title>Github Actions为什么这么香 - CC康纳百川</title>
  
    <meta name="keywords" content="Github Actions">
  

  

  <!-- feed -->
  

  <!-- import meta -->
  

  <!-- link -->
  <link rel='dns-prefetch' href='https://cdn.jsdelivr.net'>
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14/css/all.min.css">

  
  

  

  

  

  

  <!-- import link -->
  

  
  
    
<link rel="stylesheet" href="/volantis/css/style.css">

  
  
  
  <!-- 脚本懒加载函数 -->
  <script>
  function loadScript(src, cb) {
    var HEAD = document.getElementsByTagName('head')[0] || document.documentElement;
    var script = document.createElement('script');
    script.setAttribute('type','text/javascript');
    if (cb) script.onload = cb;
    script.setAttribute('src', src);
    HEAD.appendChild(script);
  }
  //https://github.com/filamentgroup/loadCSS
  !function(c){"use strict";var e=function(e,t,n,r){var o,i=c.document,a=i.createElement("link");if(t)o=t;else{var d=(i.body||i.getElementsByTagName("head")[0]).childNodes;o=d[d.length-1]}var f=i.styleSheets;if(r)for(var l in r)r.hasOwnProperty(l)&&a.setAttribute(l,r[l]);a.rel="stylesheet",a.href=e,a.media="only x",function e(t){if(i.body)return t();setTimeout(function(){e(t)})}(function(){o.parentNode.insertBefore(a,t?o:o.nextSibling)});var s=function(e){for(var t=a.href,n=f.length;n--;)if(f[n].href===t)return e();setTimeout(function(){s(e)})};function u(){a.addEventListener&&a.removeEventListener("load",u),a.media=n||"all"}return a.addEventListener&&a.addEventListener("load",u),(a.onloadcssdefined=s)(u),a};"undefined"!=typeof exports?exports.loadCSS=e:c.loadCSS=e}("undefined"!=typeof global?global:this);
  </script>
  <script id="loadcss"></script>
</head>

<body>
  <header class="l_header auto shadow blur " style='opacity: 0' >
  <div class='container'>
  <div class='wrapper'>
    <div class='nav-sub'>
      <p class="title"></p>
      <ul class='switcher nav-list-h m-phone' id="pjax-header-nav-list">
        <li><a class="s-comment fas fa-comments fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
          <li><a class="s-toc fas fa-list fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
      </ul>
    </div>
		<div class="nav-main">
      
        
        <a class="title flat-box" target="_self" href='/volantis/'>
          
            <img no-lazy class='logo' src='https://cdn.jsdelivr.net/gh/ccknbc-backup/cdn/logo/logo.png'/>
          
          
          
        </a>
      

			<div class='menu navigation'>
				<ul class='nav-list-h m-pc'>
          
          
          
            
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/
                  
                  
                  
                    id="volantis"
                  >
                  <i class='fas fa-rss fa-fw'></i>博客
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/categories/
                  
                  
                  
                    id="volantiscategories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/tags/
                  
                  
                  
                    id="volantistags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/archives/
                  
                  
                  
                    id="volantisarchives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/friends/
                  
                  
                  
                    id="volantisfriends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/about/
                  
                  
                  
                    id="volantisabout"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
          
				</ul>
			</div>

      <div class="m_search">
        <form name="searchform" class="form u-search-form">
          <i class="icon fas fa-search fa-fw"></i>
          <input type="text" class="input u-search-input" placeholder="Search..." />
        </form>
      </div>

			<ul class='switcher nav-list-h m-phone'>
				
					<li><a class="s-search fas fa-search fa-fw" target="_self" href='javascript:void(0)'></a></li>
				
				<li>
          <a class="s-menu fas fa-bars fa-fw" target="_self" href='javascript:void(0)'></a>
          <ul class="menu-phone list-v navigation white-box">
            
              
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/
                  
                  
                  
                    id="volantis"
                  >
                  <i class='fas fa-rss fa-fw'></i>博客
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/categories/
                  
                  
                  
                    id="volantiscategories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/tags/
                  
                  
                  
                    id="volantistags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/archives/
                  
                  
                  
                    id="volantisarchives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/friends/
                  
                  
                  
                    id="volantisfriends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="menuitem flat-box faa-parent animated-hover" href=/volantis/about/
                  
                  
                  
                    id="volantisabout"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
            
          </ul>
        </li>
			</ul>
		</div>
	</div>
  </div>
</header>

  <div class="l_body">
    <div class="l_cover">
  
    
    
        <div class='cover-wrapper post dock' style="display: none;">
          
            <div class='cover-bg lazyload placeholder' data-bg="https://cdn.jsdelivr.net/gh/volantis-x/cdn-wallpaper-minimalist/2020/033.jpg"></div>
          
          <div class='cover-body'>
  <div class='top'>
    
    
      <p class="title">Volantis</p>
    
    
  </div>
  <div class='bottom'>
    <div class='menu navigation'>
      <div class='list-h'>
        
          
            <a href="/volantis/v4/getting-started/"
              
              
              id="volantisv4getting-started">
              <img src='https://cdn.jsdelivr.net/gh/twitter/twemoji@13.0/assets/svg/1f5c3.svg'><p>文档</p>
            </a>
          
            <a href="/volantis/faqs/"
              
              
              id="volantisfaqs">
              <img src='https://cdn.jsdelivr.net/gh/twitter/twemoji@13.0/assets/svg/1f516.svg'><p>帮助</p>
            </a>
          
            <a href="/volantis/examples/"
              
              
              id="volantisexamples">
              <img src='https://cdn.jsdelivr.net/gh/twitter/twemoji@13.0/assets/svg/1f396.svg'><p>示例</p>
            </a>
          
            <a href="/volantis/contributors/"
              
              
              id="volantiscontributors">
              <img src='https://cdn.jsdelivr.net/gh/twitter/twemoji@13.0/assets/svg/1f389.svg'><p>社区</p>
            </a>
          
            <a href="/volantis/archives/"
              
              
              id="volantisarchives">
              <img src='https://cdn.jsdelivr.net/gh/twitter/twemoji@13.0/assets/svg/1f4f0.svg'><p>博客</p>
            </a>
          
            <a target="_blank" rel="noopener" href="https://github.com/volantis-x/hexo-theme-volantis/"
              
              
              id="https:githubcomvolantis-xhexo-theme-volantis">
              <img src='https://cdn.jsdelivr.net/gh/twitter/twemoji@13.0/assets/svg/1f9ec.svg'><p>源码</p>
            </a>
          
        
      </div>
    </div>
  </div>
</div>

          <div class="scroll-down" style="display: none;"><i class="fa fa-chevron-down scroll-down-effects"></i></div>
        </div>
    
  
  </div>
  
    <div class='safearea'>
      <div class='body-wrapper' id="pjax-container">
        
          <!--此文件用来存放一些不方便取值的变量--> 
<!--思路大概是将值藏到重加载的区域内--> 
 
 
 
<div id="pjax-data" style="display: none"> 
  <div id="pjax-ispage">true</div> 
  <div id="pjax-pageTitle">Github Actions为什么这么香</div> 
  <div id="pjax-enable-cover">true</div> 
  <div id="pjax-comment-path"></div> 
  <div id="pjax-comment-placeholder"></div> 
</div> 
 
 
<script> 
  // 处理封面 此时 jquery 还没加载 
  if ("half" == "none") { // 移除封面 
    document.getElementsByClassName('cover-wrapper')[0].style.display = "none"; 
    document.getElementsByClassName('l_header', 'cover-wrapper')[0].classList.add("show"); 
  } else { 
    if ("half" == "half") { // 半屏 
      document.getElementsByClassName('cover-wrapper')[0].setAttribute('id', 'half'); 
      document.getElementsByClassName('scroll-down')[0].style.display = "none"; 
    } else if ("half" == "full") { // 全屏 
      document.getElementsByClassName('cover-wrapper')[0].setAttribute('id', 'full'); 
      document.getElementsByClassName('scroll-down')[0].style.display = ""; 
    } 
    document.getElementsByClassName('cover-wrapper')[0].style.display = ""; 
    document.getElementsByClassName('l_header', 'cover-wrapper')[0].classList.remove("show"); 
  } 
</script> 
 

        
        

<div class='l_main'>
  <article class="article post white-box reveal md shadow article-type-post" id="post" itemscope itemprop="blogPost">
  


  
  <div class="article-meta" id="top">
    
    
    
      <h1 class="title">
        Github Actions为什么这么香
      </h1>
      <div class='new-meta-box'>
        
          
            
<div class='new-meta-item author'>
  <a class='author' href="/" rel="nofollow">
    <img no-lazy src="">
    <p>请设置文章作者</p>
  </a>
</div>

          
        
          
            
  <div class='new-meta-item category'>
    <a class='notlink'>
      <i class="fas fa-folder-open fa-fw" aria-hidden="true"></i>
      <a class="category-link" href="/volantis/categories/Github-Actions/">Github Actions</a>
    </a>
  </div>


          
        
          
            <div class="new-meta-item date">
  <a class='notlink'>
    <i class="fas fa-calendar-alt fa-fw" aria-hidden="true"></i>
    <p>发布于：2020年10月6日</p>
  </a>
</div>

          
        
          
            
  <div class="new-meta-item browse leancloud">
    <a class='notlink'>
      
      <div id="lc-pv" data-title="Github Actions为什么这么香" data-path="/volantis/posts/9/">
        <i class="fas fa-eye fa-fw" aria-hidden="true"></i>
        <span id='number'><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span>
        次浏览
      </div>
    </a>
  </div>


          
        
      </div>
    
  </div>


  
  <p><code>Github Actions</code>为什么这么香,我为什么专门建了一个组织<code>ccknbc-actions</code>来跑actions并储存博客源码，或者其他只是单纯来跑actions的项目，一开始就单纯只是想分类，forked一堆，主账号自己的项目却混在其中，最新的反而是天天运行actions的（排在前面）</p>
<p>而对大多数博主来说，都是把自己的源码（包含源文章）仓库私有起来，而一个普通账号每个月私有仓库运行时间为2000分钟，确实，是完全够用的，对于我们这种月更博主来说一个月100分钟而已（<del>好吧我就是懒</del>）；不过作为白嫖党我还是公开了仓库（这样就不限制运行时间了），但如果你选择了定时执行某些项目，注意间隔最小为<code>5分钟</code>，而触发方式也是多种多样的，但最让人欣喜的是手动触发方式终于完成了技术革命，不再监控star动作了，而是只有项目所有者可执行，这样就不会再运行记录里出现一堆无关记录影响查看（虽然之前判断仓库所有者也是OK，但是强迫症不接受两秒运行跳过无关记录）</p>
<p>而且由于分配的临时机性能还不错，速度还是很可观的，并且不受长城防火墙的限制，可以很方便的做一些其他事情，这里就不细说，回到标题，它为什么这么香呢？</p>
<p>可能在工作流文件书写上没其他第三方简单，用过<code>Travis CI</code>或者<code>Gitlab CI</code>的都应该有体会，但自家的经过这么久的发展，用起来还是比较舒服的，而且用户编写的actions可以直接拿来用发布到市场，这样有些功能的实现就不用再自己想半天去实现，几行代码就搞定，用HEXO写博客的都知道Gitee托管静态网页要更新的话就得手动点一下更新才行，而actions就可以帮我们去点一下以实现同步全自动更新，多平台部署</p>
<p>Github本身自带的模板是OK的，但毕竟很多新手完全不懂，但其实上手一段时间就明白了每一步在做什么为什么要这样写，官方文档的例子虽然很简单但确实是包含了常用的一些步骤，再加上很多人分享，直接拿来用就行，毕竟搜索引擎不能摆在那不用，爬几篇文章学习一下就会了，这里我以部署HEXO博客为例来说一下</p>
<p>首先我们得明白，要让Github能在你git push上去后能自动部署博客，你得知道博客的源文件是必须要的，但备份哪些呢，下图只是以本站做一个示例，框中的是必须要有的（如果你不打算修改主题源码其实也不用备份，但毕竟可以节约时间，第一次备份前记得在主题文件夹删掉<code>.git</code>隐藏文件夹，不然推上去是空的会导致运行失败），其他根据个人需求，，配一个证书或者README等，但你还在疑惑；我没有.github文件夹啊，没错这个就是我们自己新建的文件夹，在博客根目录依次建立<code>.github/workflows/*.yml</code>文件</p>
<p><img src="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~11:57:17.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~11:57:17.png" srcset="" alt="备份源文件" loading="lazy"></p>
<p>就像下图这样，工作流文件.yml你可以随便命名啦，然后我们在工作流文件中添加如下内容</p>
<p><img src="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~12:03:33.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/ccknbc-backup/photos/blog/2020-10-06~12:03:33.png" srcset="" alt="目录" loading="lazy"></p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">name:</span> <span class="string">Update</span> <span class="string">Blog</span> <span class="string">Butterfly</span> <span class="string">Site</span></span><br><span class="line"></span><br><span class="line"><span class="attr">on:</span> </span><br><span class="line">  <span class="attr">push:</span></span><br><span class="line">    <span class="attr">branches:</span> </span><br><span class="line">      <span class="bullet">-</span> <span class="string">master</span></span><br><span class="line">  <span class="attr">workflow_dispatch:</span></span><br><span class="line">    <span class="attr">inputs:</span></span><br><span class="line">      <span class="attr">name:</span></span><br><span class="line">        <span class="attr">description:</span> <span class="string">&#x27;手动触发&#x27;</span></span><br><span class="line">        <span class="attr">required:</span> <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="attr">jobs:</span></span><br><span class="line">  <span class="attr">deploy:</span></span><br><span class="line">    <span class="attr">runs-on:</span> <span class="string">ubuntu-latest</span></span><br><span class="line"></span><br><span class="line">    <span class="attr">steps:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Checkout</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">actions/checkout@v2</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">        <span class="attr">ref:</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Setup</span> <span class="string">Node</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">actions/setup-node@v1</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">        <span class="attr">node-version:</span> <span class="string">&quot;12.x&quot;</span></span><br><span class="line">        </span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">HEXO</span> <span class="string">CI</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">npm</span> <span class="string">install</span> <span class="string">hexo-cli</span> <span class="string">-g</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Catch</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">actions/cache@v2</span></span><br><span class="line">      <span class="attr">id:</span> <span class="string">cache-dependencies</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">        <span class="attr">path:</span> <span class="string">node_modules</span></span><br><span class="line">        <span class="attr">key:</span> <span class="string">$&#123;&#123;runner.OS&#125;&#125;-$&#123;&#123;hashFiles(&#x27;**/package-lock.json&#x27;)&#125;&#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">Dependencies</span></span><br><span class="line">      <span class="attr">if:</span> <span class="string">steps.cache-dependencies.outputs.cache-hit</span> <span class="type">!=</span> <span class="string">&#x27;true&#x27;</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">npm</span> <span class="string">install</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Generate</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">rm</span> <span class="string">-rf</span> <span class="string">./public</span></span><br><span class="line">        <span class="string">rm</span> <span class="string">-rf</span> <span class="string">./db.json</span></span><br><span class="line">        <span class="string">rm</span> <span class="string">-rf</span> <span class="string">./.deploy_git</span></span><br><span class="line">        <span class="string">hexo</span> <span class="string">-v</span></span><br><span class="line">        <span class="string">hexo</span> <span class="string">clean</span></span><br><span class="line">        <span class="string">hexo</span> <span class="string">generate</span></span><br><span class="line">        <span class="string">gulp</span></span><br><span class="line">    </span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Deploy</span></span><br><span class="line">      <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line">        <span class="string">cd</span> <span class="string">./public</span></span><br><span class="line">        <span class="string">git</span> <span class="string">init</span></span><br><span class="line">        <span class="string">git</span> <span class="string">config</span> <span class="string">--global</span> <span class="string">user.name</span> <span class="string">&quot;ccknbc&quot;</span></span><br><span class="line">        <span class="string">git</span> <span class="string">config</span> <span class="string">--global</span> <span class="string">user.email</span> <span class="string">&#x27;$<span class="template-variable">&#123;&#123; secrets.GIT_EMAIL &#125;&#125;</span>&#x27;</span></span><br><span class="line">        <span class="string">git</span> <span class="string">add</span> <span class="string">-A</span></span><br><span class="line">        <span class="string">git</span> <span class="string">commit</span> <span class="string">-m</span> <span class="string">&quot;Updated By Github Actions&quot;</span></span><br><span class="line">        <span class="string">git</span> <span class="string">push</span> <span class="string">--force</span> <span class="string">--quiet</span> <span class="string">&quot;https://ccknbc:$<span class="template-variable">&#123;&#123; secrets.GH_TOKEN &#125;&#125;</span>@github.com/ccknbc/butterfly.git&quot;</span> <span class="string">master:master</span></span><br><span class="line">        <span class="string">git</span> <span class="string">push</span> <span class="string">--force</span> <span class="string">--quiet</span> <span class="string">&quot;https://ccknbc:$<span class="template-variable">&#123;&#123; secrets.GT_TOKEN &#125;&#125;</span>@gitee.com/ccknbc/butterfly.git&quot;</span> <span class="string">master:master</span></span><br><span class="line">        <span class="string">git</span> <span class="string">push</span> <span class="string">--force</span> <span class="string">--quiet</span> <span class="string">&quot;https://$<span class="template-variable">&#123;&#123; secrets.CD_USER &#125;&#125;</span>:$<span class="template-variable">&#123;&#123; secrets.CD_TOKEN &#125;&#125;</span>@e.coding.net/ccknbc/blog/butterfly.git&quot;</span> <span class="string">master:master</span></span><br><span class="line"></span><br><span class="line">    <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Build</span> <span class="string">Gitee</span> <span class="string">Pages</span></span><br><span class="line">      <span class="attr">uses:</span> <span class="string">yanglbme/gitee-pages-action@master</span></span><br><span class="line">      <span class="attr">with:</span></span><br><span class="line">          <span class="attr">gitee-username:</span> <span class="string">ccknbc</span></span><br><span class="line">          <span class="attr">gitee-password:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets.GITEE_PASSWORD</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">gitee-repo:</span> <span class="string">ccknbc/butterfly</span></span><br></pre></td></tr></table></figure>

<p>你可能有所疑惑但又看得懂一些命令，别急，请接着往下看</p>
<h2 id="GitHub-Actions-指南"><a href="#GitHub-Actions-指南" class="headerlink" title="GitHub Actions 指南"></a>GitHub Actions 指南</h2><p><code>GitHub Actions</code>使你可以直接在你的<code>GitHub</code>库中创建自定义的工作流，工作流指的就是自动化的流程，比如构建、测试、打包、发布、部署等等，也就是说你可以直接进行 <code>CI</code>（持续集成）和 <code>CD</code>（持续部署）</p>
<h2 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h2><ol>
<li><p><code>workflow</code>: 一个 <code>workflow</code> 工作流就是一个完整的过程，每个<code>workflow</code> 包含一组 <code>jobs</code>任务</p>
</li>
<li><p><code>job : jobs</code>任务包含一个或多个<code>job</code> ，每个 <code>job</code>包含一系列的 <code>steps</code> 步骤</p>
</li>
<li><p><code>step</code> : 每个 <code>step</code> 步骤可以执行指令或者使用一个 <code>action</code> 动作</p>
</li>
<li><p><code>action</code> : 每个 <code>action</code> 动作就是一个通用的基本单元</p>
</li>
</ol>
<p>说明：</p>
<ol>
<li><p>最外层的 <code>name</code> 指定了 <code>workflow</code>的名称</p>
</li>
<li><p><code>on</code> 声明了一旦发生了 <code>push</code> 操作就会触发这个 <code>workflow</code></p>
</li>
<li><p><code>jobs</code>定义了任务集，其中可以有一个或多个<code>job</code> 任务，示例中只有一个</p>
</li>
<li><p><code>runs-on</code> 声明了运行的环境</p>
</li>
<li><p><code>steps</code>定义需要执行哪些步骤</p>
</li>
<li><p>每个 <code>step</code>可以定义自己的 <code>name</code>和<code>id</code>，通过 <code>uses</code> 可以声明使用一个具体的 <code>action</code>，通过<code>run</code> 声明需要执行哪些指令。</p>
</li>
<li><p><code>$&#123;&#123;&#125;&#125;`可以使用上下文参数

`on` 声明了何时触发 `workflow`  ，它可以是：

1. 一个或多个`GitHub` 事件，比如 `push` 了一个 `commit` 、创建了一个`issue` ，产生了一次`pull request` 等等，示例：

<figure class="highlight yml"><table><tr><td class="code"><pre><span class="line"><span class="string">on:[push,pull_request]</span></span><br></pre></td></tr></table></figure>

2. 预定的时间，示例（每天零点零分触发，不过因为时差关系，是北京时间上午8点）：

<figure class="highlight yml"><table><tr><td class="code"><pre><span class="line"><span class="attr">on:</span></span><br><span class="line">  <span class="attr">schedule:</span></span><br><span class="line">    <span class="string">-cron:&#x27;0</span> <span class="number">0</span> <span class="string">*</span> <span class="string">*</span> <span class="string">*&#x27;</span></span><br></pre></td></tr></table></figure>

3. 某个外部事件。所谓外部事件触发，简而言之就是你可以通过`REST API`向 `GitHub` 发送请求去触发，具体请查阅官方文档: `repository-dispatch-event`

配置多个事件，示例：

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">on:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Trigger the workflow on push or pull request,</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># but only for the master branch</span></span><br><span class="line"></span><br><span class="line"><span class="attr">push:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">branches:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line"><span class="attr">pull_request:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">branches:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Also trigger on page_build, as well as release created events</span></span><br><span class="line"></span><br><span class="line"><span class="attr">page_build:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">release:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">types:</span> <span class="comment"># This configuration does not affect the page_build event above</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="string">created</span></span><br></pre></td></tr></table></figure>

详细文档请参考: 触发事件

## jobs

`jobs`可以包含一个或多个 `job` ，如:

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_first_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">job</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_second_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">name:</span> <span class="string">My</span> <span class="string">second</span> <span class="string">job</span></span><br></pre></td></tr></table></figure>

如果多个`job` 之间存在依赖关系，那么你可能需要使用 `needs` :

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job1:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job2:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">needs:</span> <span class="string">job1</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job3:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">needs:</span> [<span class="string">job1</span>, <span class="string">job2</span>]</span><br></pre></td></tr></table></figure>

这里的`needs`声明了`job2` 必须等待 `job1` 成功完成，`job3`必须等待 `job1` 和 `job2`依次成功完成

每个任务默认超时时间最长为 `360`分钟，你可以通过 `timeout-minutes` 进行配置:

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line">	<span class="attr">job1:</span></span><br><span class="line">		<span class="attr">timeout-minutes:</span></span><br></pre></td></tr></table></figure>

## runs-on & strategy

`runs-on` 指定了任务的 `runner` 即执行环境，`runner` 分两种：`GitHub-hosted runner` 和 `self-hosted runner` 

所谓的 `self-hosted runner` 就是用你自己的机器，但是需要 `GitHub` 能进行访问并给与其所需的机器权限，这个不在本文描述范围内，有兴趣可参考 `self-hosted runner` 

`GitHub-hosted runner` 其实就是 `GitHub` 提供的虚拟环境，目前有以下四种:

1. `windows-latest : Windows Server 2019`

2. `ubuntu-latest`或 `ubuntu-18.04`

3. `ubuntu-16.04` : `Ubuntu 16.04`

4. `macos-latest` : `macOS Catalina 10.15`

比较常见的:

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="string">runs-on:ubuntu-latest</span></span><br></pre></td></tr></table></figure>

## runs-on 多环境

有时候我们常常需要对多个操作系统、多个平台、多个编程语言版本进行测试，为此我们可以配置一个构建矩阵

例如：

<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">runs-on:</span> <span class="string">$&#123;&#123;</span> <span class="string">matrix.os</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">strategy:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">matrix:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">os:</span> [<span class="string">ubuntu-16.04</span>, <span class="string">ubuntu-18.04</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> [<span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>]</span><br></pre></td></tr></table></figure>

示例中配置了两种`os`操作系统和三种 `node`版本即总共六种情况的构建矩阵， `$&#123;&#123;matrix.os&#125;&#125;</code> 是一个上下文参数</p>
</li>
</ol>
<p><code>strategy</code> 策略，包括：</p>
<ol>
<li><p><code>matrix</code>: 构建矩阵</p>
</li>
<li><p><code>fail-fast</code> : 默认为<code>true</code> ，即一旦某个矩阵任务失败则立即取消所有还在进行中的任务</p>
</li>
<li><p><code>max-paraller</code> : 可同时执行的最大并发数，默认情况下 <code>GitHub</code> 会动态调整</p>
</li>
</ol>
<p>示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">runs-on:</span> <span class="string">$&#123;&#123;</span> <span class="string">matrix.os</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">strategy:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">matrix:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">os:</span> [<span class="string">macos-latest</span>, <span class="string">windows-latest</span>, <span class="string">ubuntu-18.04</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> [<span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">include:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># includes a new variable of npm with a value of 2 for the matrix leg matching the os and version</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">os:</span> <span class="string">windows-latest</span></span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> <span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="attr">npm:</span> <span class="number">2</span></span><br></pre></td></tr></table></figure>

<p><code>include</code>声明了 <code>os</code> 为 <code>windows-latest</code> 时，增加一个 <code>node</code>和<code>npm</code>分别使用特定的版本的矩阵环境</p>
<p>与<code>include</code> 相反的就是<code>exclude</code> ：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">runs-on:</span> <span class="string">$&#123;&#123;</span> <span class="string">matrix.os</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">strategy:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">matrix:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">os:</span> [<span class="string">macos-latest</span>, <span class="string">windows-latest</span>, <span class="string">ubuntu-18.04</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> [<span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>]</span><br><span class="line"></span><br><span class="line"><span class="attr">exclude:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># excludes node 4 on macOS</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">os:</span> <span class="string">macos-latest</span></span><br><span class="line"></span><br><span class="line"><span class="attr">node:</span> <span class="number">4</span></span><br></pre></td></tr></table></figure>

<p><code>exclude</code> 用来删除特定的配置项，比如这里当<code>os</code> 为 <code>macos-latest</code>，将 <code>node</code>为 4 的版本从构建矩阵中移除</p>
<h2 id="steps"><a href="#steps" class="headerlink" title="steps"></a>steps</h2><p><code>steps</code> 的通用格式类似于：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">&lt;step_name&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">uses:</span> <span class="string">&lt;action&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">with:</span></span><br><span class="line"></span><br><span class="line"><span class="string">&lt;parameter_name&gt;:</span> <span class="string">&lt;parameter_value&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">id:</span> <span class="string">&lt;step_id&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">continue-on-error:</span> <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">&lt;step_name&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">timeout-minutes:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">run:</span> <span class="string">&lt;commands&gt;</span></span><br></pre></td></tr></table></figure>

<p>每个 <code>step</code> 步骤可以有:</p>
<ol>
<li><p><code>id</code> : 每个步骤的唯一标识符</p>
</li>
<li><p><code>name</code> : 步骤的名称</p>
</li>
<li><p><code>uses</code> : 使用哪个<code>action</code></p>
</li>
<li><p><code>run</code>: 执行哪些指令</p>
</li>
<li><p><code>with</code>: 指定某个<code>action</code> 可能需要输入的参数</p>
</li>
<li><p><code>continue-on-error</code> : 设置为 <code>true</code> 允许此步骤失败<code>job</code> 仍然通过</p>
</li>
<li><p><code>timeout-minutes : step</code> 的超时时间</p>
</li>
</ol>
<h2 id="action"><a href="#action" class="headerlink" title="action"></a>action</h2><p><code>action</code> 动作通常是可以通用的，这意味着你可以直接使用别人定义好的<code>action</code></p>
<h2 id="checkout-action"><a href="#checkout-action" class="headerlink" title="checkout action"></a>checkout action</h2><p><code>checkout action</code> 是一个标准动作，当以下情况时必须且需要率先使用:</p>
<ol>
<li><p><code>workflow</code> 需要项目库的代码副本，比如构建、测试、或持续集成这些操作</p>
</li>
<li><p><code>workflow</code> 中至少有一个 <code>action</code>是在同一个项目库下定义的</p>
</li>
</ol>
<p>使用示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="string">users:action/checkout@v1</span></span><br></pre></td></tr></table></figure>

<p>如果你只想浅克隆你的库，或者只复制最新的版本，你可以在 <code>with</code>中使用<code>fetch-depth</code>声明，例如:</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="string">user:action/checkout@v1</span></span><br><span class="line">  <span class="attr">with:</span></span><br><span class="line">  	<span class="string">fetch-depth:1</span></span><br></pre></td></tr></table></figure>

<h2 id="引用-action"><a href="#引用-action" class="headerlink" title="引用 action"></a>引用 action</h2><ol>
<li><p>官方<code>action</code>标准库: github.com/actions</p>
</li>
<li><p>社区库:<code>marketplace</code></p>
</li>
</ol>
<h2 id="引用公有库中的-action"><a href="#引用公有库中的-action" class="headerlink" title="引用公有库中的 action"></a>引用公有库中的 action</h2><p>引用 <code>action</code> 的格式是<code>&#123;owner&#125;/&#123;repo&#125;@&#123;ref&#125;</code>或 <code>&#123;owner&#125;/&#123;repo&#125;/&#123;path&#125;@&#123;ref&#125;</code>，例如上例的中<code>actions/checkout@v1</code>，你还可以使用标准库中的其它 <code>action</code>，如设置 <code>node</code> 版本:</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_first_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">name:</span> <span class="string">My</span> <span class="string">Job</span> <span class="string">Name</span></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">uses:</span> <span class="string">actions/setup-node@v1</span></span><br><span class="line"></span><br><span class="line"><span class="attr">with:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">node-version:</span> <span class="number">10.</span><span class="string">x</span></span><br></pre></td></tr></table></figure>

<h2 id="引用同一个库中的-action"><a href="#引用同一个库中的-action" class="headerlink" title="引用同一个库中的 action"></a>引用同一个库中的 action</h2><p>引用格式：<code>&#123;owner&#125;/&#123;repo&#125;@&#123;ref&#125;</code>或 <code>./path/to/dir</code></p>
<p>例如项目文件结构为：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="string">--</span> <span class="string">hello-world</span> <span class="string">(repository)</span></span><br><span class="line"></span><br><span class="line"> <span class="string">|__</span> <span class="string">.github</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">workflows</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">my-first-workflow.yml</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">actions</span></span><br><span class="line"></span><br><span class="line"> <span class="string">|__</span> <span class="string">hello-world-action</span></span><br><span class="line"></span><br><span class="line"> <span class="string">└──</span> <span class="string">action.yml</span></span><br></pre></td></tr></table></figure>

<p>当你想要在<code>workflow</code> 中引用自己的<code>action</code> 时可以：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">build:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">runs-on:</span> <span class="string">ubuntu-latest</span></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># This step checks out a copy of your repository.</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">uses:</span> <span class="string">actions/checkout@v1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># This step references the directory that contains the action.</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">uses:</span> <span class="string">./.github/actions/hello-world-action</span></span><br></pre></td></tr></table></figure>

<h2 id="引用-Docker-Hub-上的-container"><a href="#引用-Docker-Hub-上的-container" class="headerlink" title="引用 Docker Hub 上的 container"></a>引用 Docker Hub 上的 container</h2><p>如果某个 <code>action</code> 定义在了一个<code>docker container image</code> 中且推送到了<code>Docker Hub</code> 上，你也可以引入它，格式是<code>docker://&#123;image&#125;:&#123;tag&#125;</code> ，示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">my_first_job:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">step</span></span><br><span class="line"></span><br><span class="line"><span class="attr">uses:</span> <span class="string">docker://alpine:3.8</span></span><br></pre></td></tr></table></figure>

<p>更多信息参考:<code>Docker-image.yml workflow</code> 和<code>Creating a Docker container ``action</code></p>
<h2 id="构建-actions"><a href="#构建-actions" class="headerlink" title="构建 actions"></a>构建 actions</h2><p>请参考：<code>building-actions</code></p>
<h2 id="env"><a href="#env" class="headerlink" title="env"></a>env</h2><p>环境变量可以配置在以下地方:</p>
<ol>
<li><p><code>env</code></p>
</li>
<li><p><code>jobs.&lt;job_id&gt;.env</code></p>
</li>
<li><p><code>jobs.&lt;job_id&gt;.steps.env</code></p>
</li>
</ol>
<p>示例：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">NODE_ENV:</span> <span class="string">dev</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="attr">jobs:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">job1:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">NODE_ENV:</span> <span class="string">test</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">NODE_ENV:</span> <span class="string">prod</span></span><br></pre></td></tr></table></figure>

<p>如果重复，优先使用最近的那个</p>
<h2 id="if-amp-context"><a href="#if-amp-context" class="headerlink" title="if &amp; context"></a>if &amp; context</h2><p>你可以在 <code>job</code>和<code>step</code>中使用<code>if</code>条件语句，只有满足条件时才执行具体的<code>job</code> 或 <code>step</code> :</p>
<ol>
<li><p><code>jobs.&lt;job_id&gt;.if</code></p>
</li>
<li><p><code>jobs.&lt;job_id&gt;.steps.if</code></p>
</li>
</ol>
<p>任务状态检查函数:</p>
<ol start="3">
<li><p><code>success()</code> : 当上一步执行成功时返回 <code>true</code></p>
</li>
<li><p><code>always()</code> : 总是返回 <code>true</code></p>
</li>
<li><p><code>cancelled()</code> : 当 <code>workflow</code>被取消时返回 <code>true</code></p>
</li>
<li><p><code>failure()</code> : 当上一步执行失败时返回 <code>true</code></p>
</li>
</ol>
<p>例如：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">step1</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">always()</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">step2</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">success()</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">step3</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">failure()</span></span><br></pre></td></tr></table></figure>

<p>意思就是 <code>step1</code>总是执行，<code>step2</code> 需要上一步执行成功才执行，<code>step3</code> 只有当上一步执行失败才执行</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="string">$&#123;&#123;&lt;expression&gt;&#125;&#125;</span></span><br></pre></td></tr></table></figure>

<p>上下文和表达式: <code>$&#123;&#123;<expression>&#125;&#125; </code></p>
<p>有时候我们需要与第三方平台进行交互，这时候通常需要配置一个<code>token</code>，但是显然这个 <code>token</code> 不可能明文使用，这种个情况下我们要做的就是：</p>
<ol>
<li>在具体 <code>repository</code> 库<code>Settings</code> 的 <code>Secrets</code> 中添加一个密钥，如 <code>SOMEONE_TOKEN</code></li>
</ol>
<p><code>注：关于如何获取github或者其他平台的密钥及授权不是本文重点，大家可以自行百度</code></p>
<ol start="2">
<li>然后在<code>workflow</code>中就可以通过 <code>$&#123;&#123;secrets.SOMEONE_TOKEN&#125;&#125;</code> 将 <code>token</code>安全地传递给环境变量</li>
</ol>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">action</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line"></span><br><span class="line"><span class="attr">SOMEONE_TOKEN:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets.SOMEONE_TOKEN</span> <span class="string">&#125;&#125;</span></span><br></pre></td></tr></table></figure>

<p>这里的<code>secrets</code> 就是一个上下文，除此之外还有很多，比如：</p>
<ol>
<li><p><code>github.event_name</code> : 触发<code>workflow</code>的事件名称</p>
</li>
<li><p><code>job.status</code> : 当前<code>job</code> 的状态，如 <code>success, failure, or cancelled</code></p>
</li>
<li><p><code>steps.&lt;step id&gt;.outputs</code> : 某个 <code>action</code> 的输出</p>
</li>
<li><p><code>runner.os : runner</code>的操作系统如 <code>Linux, Windows, or macOS</code></p>
</li>
</ol>
<p>这里只列举了少数几个</p>
<p>另外在<code>if</code> 中使用时不需要 <code>$&#123;&#123;&#125;&#125;</code> 符号，比如：</p>
<figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">steps:</span></span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> <span class="attr">name:</span> <span class="string">My</span> <span class="string">first</span> <span class="string">step</span></span><br><span class="line"></span><br><span class="line"><span class="attr">if:</span> <span class="string">github.event_name</span> <span class="string">==</span> <span class="string">&#x27;pull_request&#x27;</span> <span class="string">&amp;&amp;</span> <span class="string">github.event.action</span> <span class="string">==</span> <span class="string">&#x27;unassigned&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">run:</span> <span class="string">echo</span> <span class="string">This</span> <span class="string">event</span> <span class="string">is</span> <span class="string">a</span> <span class="string">pull</span> <span class="string">request</span> <span class="string">that</span> <span class="string">had</span> <span class="string">an</span> <span class="string">assignee</span> <span class="string">removed.</span></span><br></pre></td></tr></table></figure>

<p>上下文和表达式详细信息请参考：<code>contexts-and-expression</code></p>
<p>看到这里你应该对示例文件有了一定的理解，并且想自己去实验一下了，那就去actions页面找到官方示例去体验一下吧</p>
<p>但对于为什么要备份<code>package.json</code>我想大家还是心存疑惑，那我们看看具体内容吧</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;hexo-site&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;version&quot;</span>: <span class="string">&quot;0.0.0&quot;</span>,</span><br><span class="line">  <span class="attr">&quot;private&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;build&quot;</span>: <span class="string">&quot;hexo generate&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;clean&quot;</span>: <span class="string">&quot;hexo clean&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;deploy&quot;</span>: <span class="string">&quot;hexo deploy&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;server&quot;</span>: <span class="string">&quot;hexo server&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;hexo&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;version&quot;</span>: <span class="string">&quot;5.2.0&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;dependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;cheerio&quot;</span>: <span class="string">&quot;^1.0.0-rc.3&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-htmlmin&quot;</span>: <span class="string">&quot;^5.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo&quot;</span>: <span class="string">&quot;^5.2.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-baidupush&quot;</span>: <span class="string">&quot;^1.0.2&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-charts&quot;</span>: <span class="string">&quot;^1.0.3&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-cli&quot;</span>: <span class="string">&quot;^4.2.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-deployer-git&quot;</span>: <span class="string">&quot;^2.1.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-archive&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-baidu-sitemap&quot;</span>: <span class="string">&quot;^0.1.9&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-category&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-feed&quot;</span>: <span class="string">&quot;^3.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-index&quot;</span>: <span class="string">&quot;^2.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-search&quot;</span>: <span class="string">&quot;^2.4.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-sitemap&quot;</span>: <span class="string">&quot;^2.1.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-generator-tag&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-render-pug&quot;</span>: <span class="string">&quot;^2.1.4&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-ejs&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-marked&quot;</span>: <span class="string">&quot;^3.3.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-pug&quot;</span>: <span class="string">&quot;^1.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-renderer-stylus&quot;</span>: <span class="string">&quot;^2.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-server&quot;</span>: <span class="string">&quot;^2.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-tag-aplayer&quot;</span>: <span class="string">&quot;^3.0.4&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;hexo-wordcount&quot;</span>: <span class="string">&quot;^6.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;pump&quot;</span>: <span class="string">&quot;^3.0.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;terser&quot;</span>: <span class="string">&quot;^4.8.0&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">&quot;devDependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;gulp&quot;</span>: <span class="string">&quot;4.0.2&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-cli&quot;</span>: <span class="string">&quot;2.3.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-clean-css&quot;</span>: <span class="string">&quot;^4.3.0&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-htmlclean&quot;</span>: <span class="string">&quot;^2.7.22&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;gulp-uglify&quot;</span>: <span class="string">&quot;^3.0.2&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们看到里面包含了npm脚本和一些依赖（包含dev版本的），有了这个文件我们只需执行npm i(install)即可自动安装所以所需依赖插件，而不用一个一个去敲一堆命令啦，理解了这个简单的例子，大家在部署其他项目时也许能更得心应手吧</p>
<p>今天的分享就到这里结束了，本文还有很多纰漏还请读者指出，后续将更正，下篇文章见</p>

  
  
    
    <div class='footer'>
      
      
      
        <div class='copyright'>
          <blockquote>
            
              
                <p>博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</p>

              
            
              
                <p>本文永久链接是：<a href=https://ccknbc.github.io/volantis/posts/9/>https://ccknbc.github.io/volantis/posts/9/</a></p>
              
            
          </blockquote>
        </div>
      
      
    </div>
  
  
    


  <div class='article-meta' id="bottom">
    <div class='new-meta-box'>
      
        
          <div class="new-meta-item date" itemprop="dateUpdated" datetime="2020-10-06T04:00:00+00:00">
  <a class='notlink'>
    <i class="fas fa-edit fa-fw" aria-hidden="true"></i>
    <p>更新于：2020年10月6日</p>
  </a>
</div>

        
      
        
          
  
  <div class="new-meta-item meta-tags"><a class="tag" href="/volantis/tags/Github-Actions/" rel="nofollow"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i><p>Github Actions</p></a></div>


        
      
        
          
  <div class="new-meta-item share -mob-share-list">
  <div class="-mob-share-list share-body">
    
      
        <a class="-mob-share-qq" title="" rel="external nofollow noopener noreferrer noopener"
          
          target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://ccknbc.github.io/volantis/posts/9/&title=Github Actions为什么这么香 - CC康纳百川&summary="
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qq.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qq.png" srcset="">
          
        </a>
      
    
      
        <a class="-mob-share-qzone" title="" rel="external nofollow noopener noreferrer noopener"
          
          target="_blank" href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://ccknbc.github.io/volantis/posts/9/&title=Github Actions为什么这么香 - CC康纳百川&summary="
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qzone.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/qzone.png" srcset="">
          
        </a>
      
    
      
        <a class="-mob-share-weibo" title="" rel="external nofollow noopener noreferrer noopener"
          
          target="_blank" href="http://service.weibo.com/share/share.php?url=https://ccknbc.github.io/volantis/posts/9/&title=Github Actions为什么这么香 - CC康纳百川&summary="
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/weibo.png" class="lazyload" data-srcset="https://cdn.jsdelivr.net/gh/volantis-x/cdn-org/logo/128/weibo.png" srcset="">
          
        </a>
      
    
      
    
      
    
  </div>
</div>



        
      
    </div>
  </div>


  
  

  
    <div class="prev-next">
      
        <a class='prev' href='/volantis/posts/10/'>
          <p class='title'><i class="fas fa-chevron-left" aria-hidden="true"></i>如何在中国大陆享受JOOX音乐服务</p>
          <p class='content'>本篇教程不需要科学上网工具，但会提及，并且JOOX APP之后是可以全球不受限制听歌哦
在开始之前你可能需要知道JOOX什么，看了标题你知道它能提供流媒体音乐服务，但你需要了解一下它的身世
JO...</p>
        </a>
      
      
        <a class='next' href='/volantis/posts/8/'>
          <p class='title'>我为什么选择了Tidio做聊天系统<i class="fas fa-chevron-right" aria-hidden="true"></i></p>
          <p class='content'>使用了很多聊天系统后，在这里做一个心得总结，虽然至今无人问津，但还是要坚持下去，有一天会有有缘人上门的
以以下几个为例：Chatra, Tidio, Daovoice, Gitter, Cris...</p>
        </a>
      
    </div>
  
</article>


  

  






</div>
<aside class='l_side'>
  
  
    
    



  <section class="widget toc-wrapper shadow desktop mobile" id="toc-div" >
    
  <header>
    
      <i class="fas fa-list fa-fw" aria-hidden="true"></i><span class='name'>本文目录</span>
    
  </header>


    <div class='content'>
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#GitHub-Actions-%E6%8C%87%E5%8D%97"><span class="toc-text">GitHub Actions 指南</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-text">基本概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#steps"><span class="toc-text">steps</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#action"><span class="toc-text">action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#checkout-action"><span class="toc-text">checkout action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8-action"><span class="toc-text">引用 action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E5%85%AC%E6%9C%89%E5%BA%93%E4%B8%AD%E7%9A%84-action"><span class="toc-text">引用公有库中的 action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E5%BA%93%E4%B8%AD%E7%9A%84-action"><span class="toc-text">引用同一个库中的 action</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8-Docker-Hub-%E4%B8%8A%E7%9A%84-container"><span class="toc-text">引用 Docker Hub 上的 container</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA-actions"><span class="toc-text">构建 actions</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#env"><span class="toc-text">env</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#if-amp-context"><span class="toc-text">if &amp; context</span></a></li></ol>
    </div>
  </section>


  


</aside>



      </div>
      
  
  <footer class="footer clearfix">
    <br><br>
    
      
        <div class="aplayer-container">
          


        </div>
      
    
      
        <br>
        <div class="social-wrapper">
          
            
          
            
          
            
          
        </div>
      
    
      
        <div><p>博客内容遵循 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</a></p>
</div>
      
    
      
        
          <div><p><span id="lc-sv">本站总访问量为 <span id='number'><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span> 次</span> <span id="lc-uv">访客数为 <span id='number'><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span> 人</span></p>
</div>
        
      
    
      
        本站使用
        <a href="https://github.com/volantis-x/hexo-theme-volantis/tree/4.0.0-rc.4" target="_blank" class="codename">Volantis</a>
        作为主题
      
    
      
        <div class='copyright'>
        <p><a href="/">Copyright © 2017-2020 XXX</a></p>

        </div>
      
    
  </footer>


      <a class="s-top fas fa-arrow-up fa-fw" href='javascript:void(0)'></a>
    </div>
  </div>
  <div>
    <!-- required -->

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5/dist/jquery.min.js"></script>

<script>
  function pjax_fancybox() {
    $(".md .gallery").find("img").each(function () { //渲染 fancybox
      var element = document.createElement("a"); // a 标签
      $(element).attr("class", "fancybox");
      $(element).attr("pjax-fancybox", "");  // 过滤 pjax
      $(element).attr("href", $(this).attr("src"));
      if ($(this).attr("data-original")) {
        $(element).attr("href", $(this).attr("data-original"));
      }
      $(element).attr("data-fancybox", "images");
      var caption = "";   // 描述信息
      if ($(this).attr('alt')) {  // 判断当前页面是否存在描述信息
        $(element).attr('data-caption', $(this).attr('alt'));
        caption = $(this).attr('alt');
      }
      var div = document.createElement("div");
      $(div).addClass("fancybox");
      $(this).wrap(div); // 最外层套 div ，其实主要作用还是 class 样式
      var span = document.createElement("span");
      $(span).addClass("image-caption");
      $(span).text(caption); // 加描述
      $(this).after(span);  // 再套一层描述
      $(this).wrap(element);  // 最后套 a 标签
    })
    $(".md .gallery").find("img").fancybox({
      selector: '[data-fancybox="images"]',
      hash: false,
      loop: false,
      closeClick: true,
      helpers: {
        overlay: {closeClick: true}
      },
      buttons: [
        "zoom",
        "close"
      ]
    });
  };
  function SCload_fancybox() {
    if ($(".md .gallery").find("img").length == 0) return;
    loadCSS("https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css", document.getElementById("loadcss"));
    setTimeout(function() {
      loadScript('https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js', pjax_fancybox)
    }, 1);
  };
  $(function () {
    SCload_fancybox();
  });
</script>


<!-- internal -->







  <script defer src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.1.0/dist/lazyload.min.js"></script>
<script>
  // https://www.npmjs.com/package/vanilla-lazyload
  // Set the options globally
  // to make LazyLoad self-initialize
  window.lazyLoadOptions = {
    elements_selector: ".lazyload",
    threshold: 0
  };
  // Listen to the initialization event
  // and get the instance of LazyLoad
  window.addEventListener(
    "LazyLoad::Initialized",
    function (event) {
      window.lazyLoadInstance = event.detail.instance;
    },
    false
  );
  document.addEventListener('DOMContentLoaded', function () {
    lazyLoadInstance.update();
  });
  document.addEventListener('pjax:complete', function () {
    lazyLoadInstance.update();
  });
</script>




  
  
    <script>
      window.FPConfig = {
        delay: 0,
        ignoreKeywords: [],
        maxRPS: 5,
        hoverDelay: 25
      };
    </script>
    <script defer src="https://cdn.jsdelivr.net/gh/gijo-varghese/flying-pages@2.1.2/flying-pages.min.js"></script>
  















  
<script src="/volantis/js/app.js"></script>




  
    
<script src="/volantis/js/search.js"></script>

  


<!-- optional -->

  <script>
const SearchServiceimagePath="https://cdn.jsdelivr.net/gh/volantis-x/cdn-volantis@master/img/";
const ROOT =  ("/volantis/" || "/").endsWith('/') ? ("/volantis/" || "/") : ("/volantis//" || "/" );
(function ($) {
  
    customSearch = new HexoSearch({
      imagePath: SearchServiceimagePath
    });
  
})(jQuery);

</script>











  <script defer>

  const LCCounter = {
    app_id: 'yPKOAo6M4zHWCq3jzS0iqLEW-MdYXbMMI',
    app_key: 'Vt6syMPvpjnscIVTQNFAX0jj',
    custom_api_server: '',

    // 查询存储的记录
    getRecord(Counter, url, title) {
      return new Promise(function (resolve, reject) {
        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({url})))
          .then(resp => resp.json())
          .then(({results, code, error}) => {
            if (code === 401) {
              throw error;
            }
            if (results && results.length > 0) {
              var record = results[0];
              resolve(record);
            } else {
              Counter('post', '/classes/Counter', {url, title: title, times: 0})
                .then(resp => resp.json())
                .then((record, error) => {
                  if (error) {
                    throw error;
                  }
                  resolve(record);
                }).catch(error => {
                console.error('Failed to create', error);
                reject(error);
              });
            }
          }).catch((error) => {
          console.error('LeanCloud Counter Error:', error);
          reject(error);
        });
      })
    },

    // 发起自增请求
    increment(Counter, incrArr) {
      return new Promise(function (resolve, reject) {
        Counter('post', '/batch', {
          "requests": incrArr
        }).then((res) => {
          res = res.json();
          if (res.error) {
            throw res.error;
          }
          resolve(res);
        }).catch((error) => {
          console.error('Failed to save visitor count', error);
          reject(error);
        });
      });
    },

    // 构建自增请求体
    buildIncrement(objectId) {
      return {
        "method": "PUT",
        "path": `/1.1/classes/Counter/${ objectId }`,
        "body": {
          "times": {
            '__op': 'Increment',
            'amount': 1
          }
        }
      }
    },

    // 校验是否为有效的 UV
    validUV() {
      var key = 'LeanCloudUVTimestamp';
      var flag = localStorage.getItem(key);
      if (flag) {
        // 距离标记小于 24 小时则不计为 UV
        if (new Date().getTime() - parseInt(flag) <= 86400000) {
          return false;
        }
      }
      localStorage.setItem(key, new Date().getTime().toString());
      return true;
    },

    addCount(Counter) {
      var enableIncr = '' === 'true' && window.location.hostname !== 'localhost';
      enableIncr = true;
      var getterArr = [];
      var incrArr = [];
      // 请求 PV 并自增
      var pvCtn = document.querySelector('#lc-sv');
      if (pvCtn || enableIncr) {
        var pvGetter = this.getRecord(Counter, 'https://ccknbc.github.io/volantis' + '/#lc-sv', 'Visits').then((record) => {
          incrArr.push(this.buildIncrement(record.objectId))
          var eles = document.querySelectorAll('#lc-sv #number');
          if (eles.length > 0) {
            eles.forEach((el,index,array)=>{
              el.innerText = record.times + 1;
              if (pvCtn) {
                pvCtn.style.display = 'inline';
              }
            })
          }
        });
        getterArr.push(pvGetter);
      }

      // 请求 UV 并自增
      var uvCtn = document.querySelector('#lc-uv');
      if (uvCtn || enableIncr) {
        var uvGetter = this.getRecord(Counter, 'https://ccknbc.github.io/volantis' + '/#lc-uv', 'Visitors').then((record) => {
          var vuv = this.validUV();
          vuv && incrArr.push(this.buildIncrement(record.objectId))
          var eles = document.querySelectorAll('#lc-uv #number');
          if (eles.length > 0) {
            eles.forEach((el,index,array)=>{
              el.innerText = record.times + (vuv ? 1 : 0);
              if (uvCtn) {
                uvCtn.style.display = 'inline';
              }
            })
          }
        });
        getterArr.push(uvGetter);
      }

      // 请求文章的浏览数，如果是当前页面就自增
      var allPV = document.querySelectorAll('#lc-pv');
      if (allPV.length > 0 || enableIncr) {
        for (i = 0; i < allPV.length; i++) {
          let pv = allPV[i];
          let title = pv.getAttribute('data-title');
          var url = 'https://ccknbc.github.io/volantis' + pv.getAttribute('data-path');
          if (url) {
            var viewGetter = this.getRecord(Counter, url, title).then((record) => {
              // 是当前页面就自增
              let curPath = window.location.pathname;
              if (curPath.includes('index.html')) {
                curPath = curPath.substring(0, curPath.lastIndexOf('index.html'));
              }
              if (pv.getAttribute('data-path') == curPath) {
                incrArr.push(this.buildIncrement(record.objectId));
              }
              if (pv) {
                var ele = pv.querySelector('#lc-pv #number');
                if (ele) {
                  if (pv.getAttribute('data-path') == curPath) {
                    ele.innerText = (record.times || 0) + 1;
                  } else {
                    ele.innerText = record.times || 0;
                  }
                  pv.style.display = 'inline';
                }
              }
            });
            getterArr.push(viewGetter);
          }
        }
      }

      // 如果启动计数自增，批量发起自增请求
      if (enableIncr) {
        Promise.all(getterArr).then(() => {
          incrArr.length > 0 && this.increment(Counter, incrArr);
        })
      }

    },


    fetchData(api_server) {
      var Counter = (method, url, data) => {
        return fetch(`${ api_server }/1.1${ url }`, {
          method,
          headers: {
            'X-LC-Id': this.app_id,
            'X-LC-Key': this.app_key,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      this.addCount(Counter);
    },

    refreshCounter() {
      var api_server = this.app_id.slice(-9) !== '-MdYXbMMI' ? this.custom_api_server : `https://${ this.app_id.slice(0, 8).toLowerCase() }.api.lncldglobal.com`;
      if (api_server) {
        this.fetchData(api_server);
      } else {
        fetch('https://app-router.leancloud.cn/2/route?appId=' + this.app_id)
          .then(resp => resp.json())
          .then(({api_server}) => {
            this.fetchData('https://' + api_server);
          });
      }
    }

  };

  LCCounter.refreshCounter();

  document.addEventListener('pjax:complete', function () {
    LCCounter.refreshCounter();
  });
</script>



<!-- more -->


    
      


<script src="https://cdn.jsdelivr.net/npm/pjax@0.2.8/pjax.min.js"></script>

<!-- 样式位于：source/css/_third-party/pjaxanimate.styl -->

<div class="pjax-animate">
  
    <script src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js"></script>
    <div id="loading-bar-wrapper"><script>NProgress.configure({parent:"#loading-bar-wrapper",trickleSpeed: 100})</script></div>
    <script>
      window.ShowLoading = function() {
        NProgress.start();
      };
      window.HideLoading = function() {
        NProgress.done();
      }
    </script>
  
</div>

<script>
    var pjax;
    document.addEventListener('DOMContentLoaded', function () {
      pjax = new Pjax({
        elements: 'a[href]:not([href^="#"]):not([href="javascript:void(0)"]):not([pjax-fancybox])',
        selectors: [
          "title",
          "#pjax-container",
          "#pjax-header-nav-list"
        ],
        cacheBust: false,   // url 地址追加时间戳，用以避免浏览器缓存
        timeout: 5000
      });
    });

    document.addEventListener('pjax:send', function (e) {
      window.stop(); // 相当于点击了浏览器的停止按钮

      try {
        var currentUrl = window.location.pathname;
        var targetUrl = e.triggerElement.href;
        var banUrl = [""];
        if (banUrl[0] != "") {
          banUrl.forEach(item => {
            if(currentUrl.indexOf(item) != -1 || targetUrl.indexOf(item) != -1) {
              window.location.href = targetUrl;
            }
          });
        }
      } catch (error) {}

      window.subData = null; // 移除标题（用于一二级导航栏切换处）
      if (typeof $.fancybox != "undefined") {
        $.fancybox.close();    // 关闭弹窗
      }
      $('.l_header .switcher .s-search').removeClass('active'); // 关闭移动端激活的搜索框
      $('.l_header').removeClass('z_search-open'); // 关闭移动端激活的搜索框
      $('.wrapper').removeClass('sub'); // 跳转页面时关闭二级导航

      // 解绑事件 避免重复监听
      $('.s-top').unbind('click');
      $('.menu a').unbind('click');
      $(window).unbind('resize');
      $(window).unbind('scroll');
      $(document).unbind('scroll');
      $(document).unbind('click');
      $('body').unbind('click');
      window.ShowLoading();
    });

    document.addEventListener('pjax:complete', function () {
      // 关于百度统计对 SPA 页面的处理：
      // 方案一：百度统计>管理>单页应用设置中，打开开启按钮即可对SPA进行统计。 https://tongji.baidu.com/web/help/article?id=324
      // 方案二：取消注释下列代码。 https://tongji.baidu.com/web/help/article?id=235
      // 

      // 关于谷歌统计对 SPA 页面的处理：
      // 当应用以动态方式加载内容并更新地址栏中的网址时，也应该更新通过 gtag.js 存储的网页网址。
      // https://developers.google.cn/analytics/devguides/collection/gtagjs/single-page-applications?hl=zh-cn
      

      $('.nav-main').find('.list-v').not('.menu-phone').removeAttr("style",""); // 移除小尾巴的移除
      $('.menu-phone.list-v').removeAttr("style",""); // 移除小尾巴的移除
      $('script[data-pjax], .pjax-reload script').each(function () {
        $(this).parent().append($(this).remove());
      });
      try{
          if (typeof $.fancybox == "undefined") {
            SCload_fancybox();
          } else {
            pjax_fancybox();
          }
        
        
        
        
        
        
        
        
        
        
      } catch (e) {
        console.log(e);
      }
      window.HideLoading();
    });

    document.addEventListener('pjax:error', function (e) {
      window.HideLoading();
      window.location.href = e.triggerElement.href;
    });
</script>

    
  </div>
</body>
</html>
